package com.adminserver.util.responseUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;
import java.util.LinkedHashMap;


public class ResponseUtil extends LinkedHashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;    // 序列化版本号

    ResponseUtil(int code, String msg, Object data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    /**
     * 获取 code
     *
     * @return int/code
     */
    public Integer getCode() {
        return (Integer) this.get("code");
    }

    /**
     * 获取 msg
     *
     * @return String/msg
     */
    public String getMsg() {
        return (String) this.get("msg");
    }

    /**
     * 获取 data
     *
     * @return Object/data
     */
    public Object getData() {
        return (Object) this.get("data");
    }

    /**
     * 给 code 赋值
     *
     * @param code code
     * @return ResponseUtil
     */
    public ResponseUtil setCode(int code) {
        this.put("code", code);
        return this;
    }

    /**
     * 给 msg 赋值
     *
     * @param msg msg
     * @return ResponseUtil
     */
    public ResponseUtil setMsg(String msg) {
        this.put("msg", msg);
        return this;
    }

    /**
     * 给 data 赋值
     *
     * @param data data
     * @return ResponseUtil
     */
    public ResponseUtil setData(Object data) {
        this.put("data", data);
        return this;
    }

    //=====================================构建=====================================

    public static ResponseUtil set(int code, String msg, Object data) {
        return new ResponseUtil(code, msg, data);
    }

    public static ResponseUtil setE(ResponseEnum Enum) {
        return new ResponseUtil(Enum.getCode(), Enum.getMsg(), Enum.getData());
    }

    public String toJSONString() {
        return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue);
    }


    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("ResponseUtil{");
        sb.append("code=").append(getCode());
        sb.append(", msg='").append(getMsg()).append('\'');
        sb.append(", data=").append(getData());
        sb.append('}');
        return sb.toString();
    }
}
